home *** CD-ROM | disk | FTP | other *** search
- Path: in2.uu.net!interaccess!usenet
- From: brianmcg@interaccess.com (Brian V. McGroarty)
- Newsgroups: comp.lang.c
- Subject: Re: Indexfiles
- Date: 20 Mar 1996 14:18:47 GMT
- Organization: Internet Squire
- Message-ID: <4ip447$g6h@nntp.interaccess.com>
- References: <735.6653T15T2452@mailbox.swipnet.se>
- Reply-To: brianmcg@interaccess.com
- NNTP-Posting-Host: d41-isdn.nhe.interaccess.com
- X-Newsreader: Internet Squire 1.20
-
- Jonas Kjellin wrote:
- > How do i make a indexfile?
- >
- > I mean, so you can write, create, update, search, delete recordes...
-
- I will assume these records are of varying sizes. If they are not, you can
- go without the index file and simply perform an fseek to the location which
- is "n" times the size of your record to point directly at the "n"th record.
-
-
- > If i would like to find a record fast, is it better to make a special index
- > file who only have the positions for the records in the datafile or?
- >
- > I mean i would like it as a database.. byt how do i do it fast, so i dont
- > need to start from the beginning of the file and then read every record
- > and compare if it matches my keys..?
-
- The structure of such a file would be that the index file is simply an
- array of offsets into the file at which each record is located. To seek to
- the "n"th record you would seek to the "n"th entry in the index file, i.e
-
- fseek( yourFile, sizeof( indexEntry )*n, SEEK_SET )
-
- Here, you read the indexEntry data item (presumably an integer type) and
- seek that far into the main record file... this points you directly at the
- data record without having to do any key searching.
-
-
-
- > and how do i delete a record in the
- > middle of a big datafile? do i have to rewrite the whole file??
-
- If you wish to be able to delete entries then you should consider keeping a
- free list of some sort. One approach to this would be to keep a second
- file which is simply a listing of file offsets of and sizes of free blocks
- within the main database file. Any time you remove an item from the
- database, you would run through the list of free entries to see if you are
- "next to" another entry. If you are, you would then expand the existing
- entry to encompass the new entry. If this is done, then you will also need
- to check to see if this entry now becomes adjacent another entry which
- would also need to be merged. Now, any time you wish to create a new
- record, you would first check to see if there is an item in the free file
- which matches the size of the entry you wish to create. If not, you would
- extend the length of the file as usual.
-
-
-
-
- ---
- Brian Valters McGroarty -- brianmcg@bix.com
- phone/fax (847) 439-7714
-
-
-
-